let express = require('express');
let router = express.Router();
let session = require("../middleware/session");

router.use(session.session);

// 设置是否允许跨域
router.all("*",function(req,res,next){
  //设置允许跨域的域名，*代表允许任意域名跨域
  res.header("Access-Control-Allow-Origin","*");
  //允许的header类型
  res.header("Access-Control-Allow-Headers","content-type");
  //跨域允许的请求方式
  res.header("Access-Control-Allow-Methods","DELETE,PUT,POST,GET,OPTIONS");
  if (req.method.toLowerCase() == 'options')
    res.send(200);  //让options尝试请求快速结束
  else
    next();
})

// 用户
let userListController = require("../controller/userList");
// 账号
let accountController = require("../controller/account");
// 价格
let priceController = require("../controller/pigPrice");
// 待出售
let marketController = require("../controller/market");

/*
  定义接口地址
*/

//登录接口
router.post("/api/login", userListController.login);
//注册接口
router.post("/api/register", userListController.register);
//获取用户接口
router.post("/api/getUserList", userListController.getUserList);
//编辑用户接口
router.post("/api/editUser", userListController.editUser);
//删除用户接口
router.post("/api/delUser", userListController.delUser);

//新增账号接口
router.post("/api/addAccount", accountController.addAccount);
//查询账号接口
router.post("/api/getAccountList", accountController.getAccountList);
//删除账号接口
router.post("/api/delAccount", accountController.delAccount);
//修改账号接口
router.post("/api/editAccount", accountController.editAccount);

//新增价格
router.post("/api/addPrice", priceController.addPrice);
//获取价格
router.post("/api/getPriceList", priceController.getPriceList);
//删除价格
router.post("/api/delPrice", priceController.delPrice);

//添加待出售
router.post("/api/addMarket", marketController.addMarket);
//查询待出售
router.post("/api/getMarketList", marketController.getMarketList);

module.exports = router;
